<!doctype html>
<html class="no-js">
<head>
<% include("/common/admin_head.html",{title: '一键网站更新', formJS: true}){}%>
<script>
  $().ready( function() {
    $(".loadingBar").hide();
		
	// 根据更新选项显示/隐藏开始日期和结束日期
	$(".buildTypeInput").click( function() {
	  if ($(this).val() == "date") {
	    $(".dateTr").show();
		$("#progressid").css("width", "0%");
		$(".loadingBar").hide();
	  } else {
		$(".dateTr").hide();
		$("#progressid").css("width", "0%");
		$(".loadingBar").hide();
	  }
	});

	$("#inputForm").submit(function() {
	  $(".loadingBar").show();
	  $("#buildType").val($(".buildTypeInput:checked").val());
	  $("#maxResults").val($("#maxResultsInput").val());
	  $("#beginDate").val($("#beginDateInput").val());
	  $("#endDate").val($("#endDateInput").val());
	});

    var isInitialize = false;
	var buildTotal = 0;
	$("#inputForm").ajaxForm({
	  dataType: "json",
	  beforeSubmit: function(data) {
	    if (!isInitialize) {
		  isInitialize = true;
		  $(".buildTypeInput").attr("disabled", true);
		  $("#maxResultsInput").attr("disabled", true);
		  $("#beginDateInput").attr("disabled", true);
		  $("#endDateInput").attr("disabled", true);
		  $(":submit").attr("disabled", true);
		  $("#statusTr").show();
		  $("#status").text("正在更新BASE_JAVASCRIPT，请稍后...");
		}
	  },
	  success: function(data) {
	    if (data.buildTotal) {
		  buildTotal += Number(data.buildTotal);
		}
		if (data.status == "baseJavascriptFinish") {
		  $("#progressid").css("width", "10%");
		  $("#status").text("正在更新自定义错误页，请稍后...");
		  $("#buildContent").val("errorPage");
		  $("#inputForm").submit();
		} else if (data.status == "errorPageFinish") {
		  $("#progressid").css("width", "20%");
		  $("#status").text("正在更新首页，请稍后...");
		  $("#buildContent").val("index");
		  $("#inputForm").submit();
		} else if (data.status == "indexFinish") {
		  $("#progressid").css("width", "30%");
		  $("#status").text("正在更新登录页，请稍后...");
		  $("#buildContent").val("login");
		  $("#inputForm").submit();
		} else if (data.status == "loginFinish") {
		  $("#progressid").css("width", "50%");
		  $("#status").text("正在更新文章，请稍后...");
		  $("#buildContent").val("article");
		  $("#inputForm").submit();
		} else if (data.status == "articleBuilding") {
		  var maxResults = Number($("#maxResults").val());
		  var firstResult = Number(data.firstResult);
		  $("#progressid").css("width", "70%");
		  $("#status").text("正在更新文章[" + (firstResult + 1) + " - " + (firstResult + maxResults) + "]，请稍后...");
		  $("#buildContent").val("article");
		  $("#firstResult").val(firstResult);
		  $("#inputForm").submit();
		} else if (data.status == "articleFinish") {
		  $("#progressid").css("width", "80%");
		  $("#status").text("正在更新商品，请稍后...");
		  $("#buildContent").val("product");
	      $("#firstResult").val("0");
		  $("#inputForm").submit();
		} else if (data.status == "productBuilding") {
		  var maxResults = Number($("#maxResults").val());
		  var firstResult = Number(data.firstResult);
		  $("#progressid").css("width", "90%");
		  $("#status").text("正在更新商品[" + (firstResult + 1) + " - " + (firstResult + maxResults) + "]，请稍后...");
		  $("#buildContent").val("product");
		  $("#firstResult").val(firstResult);
		  $("#inputForm").submit();
		} else if (data.status == "productFinish") {
		  $("#buildContent").val("");
		  $("#firstResult").val("0");
		  $("#statusTr").hide();
		  $(".buildTypeInput").attr("disabled", false);
		  $("#maxResultsInput").attr("disabled", false);
		  $("#beginDateInput").attr("disabled", false);
	      $("#endDateInput").attr("disabled", false);
		  $(":submit").attr("disabled", false);
		  $("#progressid").css("width", "100%");
		  $("#status").text("网站更新成功！[更新总数: " + buildTotal + "]");
		  isInitialize = false;
		  buildTotal = 0;
		}
	  }
	});
  });
</script>
</head>
<body>
  <div class="admin-content">
    <div class="admin-content-body">
      <div class="am-cf app-padding-title">
        <div class="am-fl am-cf">
          <strong class="am-text-primary am-text-lg">模板管理</strong> / <small>一键网站更新</small>
        </div>
      </div>
      
      <hr data-am-widget="divider" class="am-divider am-divider-dotted am-margin-0" />
      
      <form id="inputForm" action="${base}/buildHtml/all" method="post">
      <input type="hidden" id="buildType" name="buildType" value="" />
	  <input type="hidden" id="maxResults" name="maxResults" value="" />
	  <input type="hidden" id="firstResult" name="firstResult" value="0" />
	  <input type="hidden" id="buildContent" name="buildContent" value="" />
	  <input type="hidden" id="beginDate" name="beginDate" value="" />
	  <input type="hidden" id="endDate" name="endDate" value="" />
      <div class="am-form am-margin am-form-horizontal">
        <div class="am-form-group am-form-group-sm">
          <label class="am-u-sm-4 am-u-md-3 am-text-right am-form-label">更新选项</label>
          <div class="am-u-sm-8 am-u-md-9">
            <label class="am-radio-inline">
              <input type="radio" name="buildTypeInput" class="buildTypeInput" value="date" checked> 指定日期
            </label>
            <label class="am-radio-inline">
              <input type="radio" name="buildTypeInput" class="buildTypeInput" value="all"> 更新所有
            </label>
          </div>
        </div>
        <div class="am-form-group am-form-group-sm">
          <label class="am-u-sm-4 am-u-md-3 am-text-right am-form-label">起始日期</label>
          <div class="am-u-sm-8 am-u-md-9">
            <div class="am-form-group am-form-icon">
	          <i class="am-icon-calendar"></i>
	          <input type="text" id="beginDateInput" name="" class="am-form-field" placeholder="起始日期" data-am-datepicker/>
	        </div>
          </div>
        </div>
        <div class="am-form-group am-form-group-sm">
          <label class="am-u-sm-4 am-u-md-3 am-text-right am-form-label">结束日期</label>
          <div class="am-u-sm-8 am-u-md-9">
            <div class="am-form-group am-form-icon">
	          <i class="am-icon-calendar"></i>
	          <input type="text" id="endDateInput" name="" class="am-form-field" placeholder="结束日期" data-am-datepicker/>
	        </div>
          </div>
        </div>
        <div class="am-form-group am-form-group-sm">
          <label class="am-u-sm-4 am-u-md-3 am-text-right am-form-label">每次更新数</label>
          <div class="am-u-sm-8 am-u-md-9">
            <input type="number" id="maxResultsInput" class="am-form-field" name="" value="50" min="0">
          </div>
        </div>
        <!-- 进度条 -->
        <div class="am-form-group am-form-group-sm loadingBar">
          <label class="am-u-sm-4 am-u-md-3 am-text-right am-form-label">&nbsp;</label>
          <div class="am-u-sm-8 am-u-md-9">
            <div class="am-progress am-progress-striped">
		      <div id="progressid"  class="am-progress-bar am-progress-bar-success" style="width: 0%"><p id="status"></p></div>
		    </div>
          </div>
        </div>
        <hr data-am-widget="divider" class="am-divider am-divider-dashed" />
        <div class="am-form-group am-form-group-sm">
          <div class="am-u-sm-8 am-u-sm-offset-4 am-u-md-9 am-u-md-offset-3">
            <button type="submit" class="am-btn am-btn-primary"><span class="am-icon-save">  提交更新</span></button>
            <button type="button" class="am-btn am-btn-default" onclick="window.history.back(); return false;"><span class="am-icon-undo">  返  回</span></button>
          </div>
        </div>
      </div>
	  </form>
	</div>
  </div>
<!-- content end -->
</body>
</html>
